package com.htfyun.factorytest.engineer_test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.VolumeUtils;
import com.htfyun.factorytest.R;
import com.htfyun.factorytest.common.RecordAndPlayThread;
import com.htfyun.factorytest.utils.DebugLog;

public class FragmentES7210_RecordAndPlay extends BaseFragment implements View.OnClickListener{

    private OnBtnResultForES7210 onBtnResultForES7210;

    public void setOnBtnResultForES7210(OnBtnResultForES7210 onBtnResultForES7210) {
        this.onBtnResultForES7210 = onBtnResultForES7210;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_engineer_es7210_record_and_play;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {

        initBtn(R.id.btnRecord);

        initBtn(R.id.btnFail);
        initBtn(R.id.btnPass);

        setLayoutBtnVisibility(false);
        updateBtnRecord();

        VolumeUtils.setVolume(AudioManager.STREAM_MUSIC, VolumeUtils.getMaxVolume(AudioManager.STREAM_MUSIC), 0);

    }

    private volatile boolean isHeadsetRegistered = false;
    @Override
    protected void onVisible() {
        super.onVisible();

        if (!isHeadsetRegistered) {
            isHeadsetRegistered = true;
            //发送广播检测耳机插入状态
            IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
            mContext.registerReceiver(headsetPlugReceiver, filter);
        }
    }

    @Override
    protected void onInvisible() {
        super.onInvisible();
        doStopRecord();
        updateBtnRecord();

        if (isHeadsetRegistered) {
            isHeadsetRegistered = false;
            mContext.unregisterReceiver(headsetPlugReceiver);
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btnRecord:
                clickBtnRecorder();
                break;
            case R.id.btnFail:
            case R.id.btnPass:

                doStopRecord();
                updateBtnRecord();

                if (onBtnResultForES7210 != null) {
                    onBtnResultForES7210.onBtnResultForES7210(v.getId() == R.id.btnPass);
                    findViewById(R.id.btnFail).setSelected(false);
                    findViewById(R.id.btnPass).setSelected(false);
                    v.setSelected(true);
                }
                break;
        }
    }

    private void setLayoutBtnVisibility(boolean visibility) {
        View layoutBtn = findViewById(R.id.layoutBtn);
        if (layoutBtn == null) {
            return;
        }
//        layoutBtn.setVisibility(visibility ? View.VISIBLE : View.INVISIBLE);
        layoutBtn.setEnabled(visibility);
        if (layoutBtn instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) layoutBtn;
            for (int i = 0; i < group.getChildCount(); i++) {
                View child = group.getChildAt(i);
                child.setEnabled(visibility);
            }
        }
    }

    private void doStopRecord() {
        stopTestRecord();
    }

    private void doStartRecord() {
        startTestRecord();
        setLayoutBtnVisibility(true);
    }

    private void clickBtnRecorder() {
        if (recorderThread != null) {
            doStopRecord();
        } else {
            doStartRecord();
        }
        updateBtnRecord();
    }


    private void initBtn(int resId) {
        View view = findViewById(resId);
        if (view != null) {
            view.setOnClickListener(this);
        }
    }

    private void updateBtnRecord() {
        Button btnRecord = (Button) findViewById(R.id.btnRecord);
        if (btnRecord == null) {
            return;
        }
        if (recorderThread != null) {
            btnRecord.setText(R.string.extra_test_stop);
        } else {
            btnRecord.setText(R.string.extra_test_start);
        }
    }

    private void setBtnRecordEnabled(boolean enabled) {
        Button btnRecord = (Button) findViewById(R.id.btnRecord);
        if (btnRecord == null) {
            return;
        }
        btnRecord.setEnabled(enabled);
    }


    //////////////////////
    //    Recorder
    /////////////////////
    private RecordAndPlayThread recorderThread;


    public void startTestRecord() {
        startRecordTread();
    }

    private void stopTestRecord() {
        stopRecordThread();

    }

    private void startRecordTread() {

        if (recorderThread != null) {
            stopRecordThread();
        }
        if (getContext() == null) {
            return;
        }

        recorderThread = new RecordAndPlayThread("pcba_rec_play", getContext(), AudioManager.STREAM_MUSIC, true);
        recorderThread.start();

    }

    private void stopRecordThread() {

        if (recorderThread != null) {
            recorderThread.joinThread();
            recorderThread = null;
        }
    }

    private BroadcastReceiver headsetPlugReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (action != null && action.equals(Intent.ACTION_HEADSET_PLUG)) {
                if (intent.hasExtra("state")) {
                    boolean unplug = (intent.getIntExtra("state", 0) == 0);
                    DebugLog.e("unplug = " + unplug);

                    setBtnRecordEnabled(unplug);

                }
            }
        }
    };

}
